<cfcomponent name="Application.cfc" hint="The application.cfc for the app" output="false">

	<cfscript>
		this.name = "%appname%";
		this.applicationTimeout = CreateTimeSpan(4, 0, 0, 0);
		this.sessionManagement = true;
		this.sessionTimeout = CreateTimeSpan(0, 1, 0, 0);
	</cfscript>
	<cfsetting showdebugoutput="false">
	
	<cffunction name="onApplicationStart" hint="Application start tag" access="public" returntype="boolean" output="false">
		<cfset loadTransferAndWebservices()>
		<cfreturn true>
	</cffunction>
	
	<cffunction name="onRequestStart">
		<cfargument name="requestname" required=true/>
		<cfparam name="url.reinit" default="">
		<cfif 	isdefined("url.reinit")
				or not isdefined("application.transferFactory") 
				or (isdefined("application.transferFactory") and isSimpleValue(application.transferFactor))>
			<cflock name="#application.applicationname#_reinit" type="exclusive" timeout="30">
				<cfscript>
				onApplicationStart();
				</cfscript>
			</cflock>				
		</cfif>
   </cffunction>
   
   <cffunction name="onError" output="true">
		<cfargument name="exception" required=true/>
		<cfargument name="eventName" type="String" required=true/>
	</cffunction>	
	
	<cffunction name="loadTransferAndWebservices">
		<cfscript>
			if(structkeyexists(application,"transferFactory") and isObject(application.transferFactory))
			{
				transferloaded 	= true;
			}
			else
			{
				transferloaded 	= false;
			}
			basefolder 		= rereplace( cgi.SCRIPT_NAME , "(.*/)(.*)" , "\1" , "ALL" );
			basefolder		= replace(basefolder, "/service","");
			reloadtransfer 	= false;
			reloadwebservices = false;
			transferloaded = false;

			if( isdefined("url.reinit") )
			{
				if(url.reinit EQ "yup" or url.reinit eq "true")
				{
					reloadtransfer 	= true;
					reloadwebservices = true;
				}
				else if(url.reinit eq "transfer")
				{
					reloadtransfer 	= true;
					reloadwebservices = false;
				}
				else if(url.reinit eq "ws")
				{
					reloadtransfer 	= false;
					reloadwebservices = true;
				}
			}
			if(not transferloaded)
			{
				// if this is server startup then don't reload the WS
				reloadtransfer 	= true;
				reloadwebservices = false;
			}
			/**
			 * create transfer factory - basic singleton
			 * All paths should start with '/' if they must be edited
			 * This points to the 2 configuration files, and the folder 
			 * to write the .transfer files to
			 */
			if(reloadtransfer)
			{	
				
				if( fileexists( expandpath("#basefolder#xml/transfer.xml") ) )
				{
					application.transferFactory = createObject("component", "transfer.TransferFactory").init(
												"#basefolder#xml/datasource.xml", 
												"#basefolder#xml/transfer.xml",
												"#basefolder#definitions/");
				}
			}
			
		</cfscript>
		<cfif reloadwebservices>
			<!--- get a directory listing of everything in the service directory --->						
			<cfdirectory action="list" directory="#expandpath('service')#" name="qservices"/>
			<!--- filter those results for only cfc files --->
			<cfquery name="qservices" dbtype="query">
				Select *
				From qservices
				Where name like '%.cfc'
			</cfquery>
			<!--- take the script name out i.e. /com/test/test.cfm and grab the path /com/test/ --->
			<cfset path=getdirectoryfrompath(cgi.script_name)>
			<!--- add service to the path so we have /com/test/service --->
			<cfset path = "#path#service/">
			<!--- loop through all the names of CFC's in the service directory
				  and refreshthe web service --->
			<cfset createObject("component","cfide.adminapi.administrator").login("tao42nu")>
			<cfset ws = createobject("component","CFIDE.adminapi.extensions")>
			<cfoutput query="qservices">
				<cfscript>
					// make the end point, i.e. http://test.com/com/test/service/zipcodeview.cfc?wsdl
					sdl   =   "http://#cgi.HTTP_HOST##path##name#?wsdl";
				</cfscript>
				<cfscript>
					// create the factory object
					factory = CreateObject('JAVA', "coldfusion.server.ServiceFactory");
					// grab the xmlrpcservice
					RpcService = factory.XmlRpcService;
					// and refresh the service
					RpcService.unregisterWebService(sdl);
				</cfscript>
				<!---<cfset ws.reloadWebService(name=sdl,path=sdl)>--->
			</cfoutput>
		</cfif>
	</cffunction>
</cfcomponent>